<template>
    <div>
        <div v-if="currentPanel == 'list'">
            <el-card class="box-card">
                <div slot="header" class="clearfix">
                    <span>客户SOP</span>
                </div>
                <div style="display:flex;justify-content: space-between">
                    <div>
                        <el-input v-model="queryData.sopName" placeholder="请输入要搜索的名称" size="small"></el-input>&nbsp;&nbsp;
                        <el-button type="primary" @click="queryDataHandle(1, 10)" size="small">查询</el-button>
<!--                        <el-divider direction="vertical"></el-divider>-->
                        <el-button type="primary" @click="resetQuery" size="small">重置</el-button>
                    </div>
                    <div>
                        <el-button type="primary" @click="currentHandle('add')" size="small">+创建规则</el-button>
                    </div>
                </div>

                <div class="interspaceH"></div>

                <el-table :data="dataSource" :header-cell-style="{background:'#F3F4F7',color:'#555'}"
                          style="width: 100%">
                    <el-table-column prop="sopName" label="规则名称"></el-table-column>
                    <el-table-column prop="creator" label="创建人"></el-table-column>
                    <el-table-column label="使用成员" width="400">
                        <template slot-scope="scope">
                            <el-tag :key="index" v-for="(item, index) in scope.row.empList" :disable-transitions="false"
                                    style="margin-right: 10px">
                                {{ item.empName }}
                            </el-tag>
                        </template>
                    </el-table-column>
                    <el-table-column prop="createTime" label="创建时间" :formatter="formatter"></el-table-column>
                    <el-table-column label="开关">
                        <template slot-scope="scope">
                            <el-switch v-model="scope.row.sopStatus" active-color="#13ce66"></el-switch>
                        </template>
                    </el-table-column>
                    <el-table-column label="操作">
                        <template slot-scope="scope">
                            <el-button type="text" size="small"
                                       @click="chooseData = scope.row;selectMemberVisible = true;">添加成员
                            </el-button>
                            <el-divider direction="vertical"></el-divider>
                            <el-button @click="chooseData = {...scope.row};currentHandle('detail');" type="text" size="small">详情</el-button>
                            <el-divider direction="vertical"></el-divider>
                            <el-button @click="updateHandle(scope.row)" type="text" size="small">编辑</el-button>
                            <el-divider direction="vertical"></el-divider>
                            <el-button @click="deleteHandle(scope.row)" type="text" size="small">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>

                <div class="interspaceH"></div>

                <el-pagination @size-change="queryDataHandle(pagePagination.currentPage,$event)"
                               @current-change="queryDataHandle($event,pagePagination.pageSize)"
                               :current-page="pagePagination.currentPage"
                               :page-sizes="pagePagination.pageSizes" :page-size="pagePagination.pageSize"
                               layout="total, sizes, prev, pager, next"
                               :total="pagePagination.total">
                </el-pagination>
            </el-card>
        </div>

        <div v-if="currentPanel == 'add'">
            <CreateSop @currentHandle="currentHandle"/>
        </div>

        <div v-show="currentPanel == 'edit'">
            <CreateSop @currentHandle="currentHandle" :sopData="chooseData" creatType="edit"/>
        </div>

        <div v-show="currentPanel == 'detail'">
            <DetailSop @currentHandle="currentHandle" :sopData="chooseData"/>
        </div>
        <el-dialog title="选择成员" :visible.sync="selectMemberVisible" width="60%" :close-on-click-modal="false"
                   :destroy-on-close="true">
            <List_multiple_select ref="selectMember" :listFields="listFields" :dataSource="memberList" type="filter"/>
            <span slot="footer" class="dialog-footer">
                <el-button @click="selectMemberVisible = false">取 消</el-button>
                <el-button type="primary" @click="addMemberHandle">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import List_multiple_select from "@/components/list_multiple_select/list_multiple_select";
import CreateSop from "@/views/customer/client_sop/add"
import DetailSop from "@/views/customer/client_sop/detail"

const queryData = {
    sopName: '',//名称
}

export default {
    components: {
        CreateSop,
        DetailSop,
        List_multiple_select
    },
    data() {
        return {
            tablePage: 0,
            currentPanel: 'list',
            selectMemberVisible: false,
            dictionary: {},
            fieldType: {
                createTime: "datetime"
            },
            queryData: {
                sopName: ''
            },
            dataSource: [],
            pagePagination: {
                currentPage: 1,
                pageSizes: [10, 15, 20, 25],
                pageSize: 10,
                total: 0
            },
            chooseData: {},
            memberList: [],
            listFields: {
                deptName: '所属部门',
                empName: '员工名称'
            },
        }
    },
    mounted() {
        this.queryDataHandle()
        this.queryMemberHandle()
    },
    methods: {
        queryDataHandle(page = 1, row = 10) {
            let payload = {
                ...this.queryData,
                sopType: '1',
                pageNum: page,
                pageSize: row
            }
            let that = this;
            that.$request("/api/sop/selectAllSop/1_0", payload, "POST").then((data) => {
                data.rows.map((item) => {
                    item['sopStatus'] = JSON.parse(item['sopStatus'])
                })
                this.dataSource = data.rows
                that.pagePagination['total'] = data.total
                that.pagePagination['pageSize'] = data.row
            })
        },
        queryMemberHandle() {
            this.$request("/api/emp/getAllEmp/1_0", {}, "POST").then((data) => {
                this.memberList = data.rows
            })
        },
        resetQuery() {
            this.queryData = {...queryData}
        },
        formatter(row, column) {
            let field = column.property;
            let value = row[field];
            return this.$utils.formatterForLable(field, value, this.fieldType, this['dictionary'][field]);
        },
        currentHandle(e) {
            if (e == 'list') {
                this.queryDataHandle()
            }
            this.currentPanel = e;
        },
        deleteHandle(row) {
            this.$confirm('是否确认删除?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$request("/api/sop/deleteSop/1_0?sopId=" + row.sopId, "", "GET").then((data) => {
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                    this.queryDataHandle()
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },
        updateHandle(row) {
            this.currentPanel = 'edit';
            this.chooseData = {...row}
        },
        addMemberHandle() {
            let data = this.chooseData
            let memberData = this.$refs.selectMember.getData();
            let empArr = data['relationId'].split(",")
            memberData.map((item) => {
                empArr.push(item.empId)
            })
            data['relationId'] = empArr.toString();
            this.$request("/api/sop/updateSop/1_0", data, "POST").then((data) => {
                this.$message.success("添加成员成功！")
                this.queryDataHandle()
                this.selectMemberVisible = false
            })
        }
    }
}
</script>

<style scoped>
.box-card .el-input {
    width: 200px;
}

.box-card .el-select {
    width: 200px;
}

a {
    color: #298bef;
}
</style>
